﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.DoctorSchedulingCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.DoctorSchedulingHandler
{
    /// <summary>
    /// 添加排班处理器
    /// </summary>
    public class AddDoctorSchedulingHandler : IRequestHandler<AddDoctorSchedulingCommand, APIResult<int>>
    {
        private readonly IBaseRepository<DoctorScheduling> baseRepository;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        /// <param name="mapper"></param>
        public AddDoctorSchedulingHandler(IBaseRepository<DoctorScheduling> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(AddDoctorSchedulingCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var entity = mapper.Map<DoctorScheduling>(request);

                entity.MorningAdmissions = "9:00";
                entity.AfternoonAdmissions = "14:00";


                var a = baseRepository.AddAsync(entity).Result;
                if (a > 0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "添加成功";
                    result.Data = a;

                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "添加失败";
                    result.Data = a;

                }

                return await Task.FromResult(result);

            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
